package com.tarena.lbs.coupon.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.coupon.api.pojo.param.UserCouponsParam;
import com.tarena.lbs.coupon.pojo.param.CouponParam;
import com.tarena.lbs.coupon.pojo.param.UpdateCouponParam;
import com.tarena.lbs.coupon.pojo.query.UserCouponCodeQuery;
import com.tarena.lbs.coupon.pojo.query.UserCouponQuery;
import com.tarena.lbs.coupon.pojo.vo.CouponVO;
import com.tarena.lbs.coupon.pojo.vo.UserCouponsVO;
import com.tarena.lbs.coupon.service.CouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.models.security.SecurityScheme;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 处理和优惠券数据业务有关的接口
 */
@RestController
@Slf4j
@Tag(name = "优惠券模块")
public class CouponController {

    @Autowired
    private CouponService couponService;


    @Operation(summary = "查询当前商家优惠券分页列表")
    @PostMapping("/admin/marketing/coupon/info/list")
    public Result<PageResult<CouponVO>> pageList() throws BusinessException {
        log.info("查询当前商家优惠券分页列表");
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        if (userPrinciple == null) {
            throw new BusinessException();
        }
        Integer userId = userPrinciple.getId();
        log.info("用户ID：{}", userId);
        PageResult<CouponVO> pageResult = couponService.pageList(userPrinciple);
        log.info("查询当前商家优惠券分页列表结果：{}", pageResult.getObjects());
        return new Result<>(pageResult);
    }


    @Operation(summary = "新增优惠券")
    @PostMapping("/admin/marketing/coupon/info/save")
    public Result<Void> addCoupon(@RequestBody CouponParam couponParam) throws BusinessException {
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        if (userPrinciple == null) {
            throw new BusinessException();
        }
        log.debug("管理员：{},id:{}，正在尝试添加优惠券",
                userPrinciple.getNickname(), userPrinciple.getId());
        couponParam.setBusinessId(userPrinciple.getId());
        return couponService.addCoupon(couponParam);
    }

    @Operation(summary = "查询优惠券详情")
    @GetMapping("/admin/marketing/coupon/info/detail/{id}")
    public Result<CouponVO> detail(@PathVariable("id") Integer id) throws BusinessException {
        CouponVO couponVO = couponService.detail(id);
        return new Result<>(couponVO);
    }


    @Operation(summary = "领取优惠券")
    @PostMapping("/marketing/user/receive/save")
    public Result<Void> receiveCoupon(@RequestBody UserCouponsParam param)
            throws BusinessException {
        Result<Boolean> result = couponService.receiveCoupon(param);
        if (!result.getData()) {
            log.info("用户领取优惠券失败：{}", result.getMessage());
        }
        return Result.success();
    }


    @Operation(summary = "用户领取优惠券列表")
    @PostMapping("/marketing/user/receive/list")
    public Result<PageResult<UserCouponsVO>> receiveList(@RequestBody UserCouponQuery couponQuery) throws BusinessException {
// 获取登录信息
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        if (userPrinciple == null) {
            log.info("用户");
            throw new IllegalArgumentException("用户未登录");
        }
        Integer userId = userPrinciple.getId();
        log.debug("用户未登录id：{} 正在尝试查询用户领券列表", userId);
        couponQuery.setUserId(userId);
        // 调用服务层获取用户优惠券列表
        PageResult<UserCouponsVO> pageResult = couponService.listUserReceivedCoupons(couponQuery, userId);
        return new Result<>(pageResult);
    }


    @Operation(summary = "查看优惠券码的二维码图片")
    @PostMapping("/marketing/user/receive/detail")
    public Result<UserCouponsVO> receiveDetail(@RequestBody UserCouponCodeQuery query) throws BusinessException {
        UserCouponsVO userCouponsVO = couponService.receiveDetail(query);
        return new Result<>(userCouponsVO);
    }

    @Operation(summary = "商家查询用户领取优惠券列表")
    @PostMapping("/admin/marketing/user/receive/list")
    public Result<PageResult<UserCouponsVO>> listUserReceivedCoupons(@RequestBody UserCouponQuery couponQuery)
            throws BusinessException {
        throw new UnsupportedOperationException("暂不支持该功能");
    }

    /**
     * 平台管理员对商家进行优惠券更新
     *
     * @return
     */
    @Operation(summary = "管理员对商家进行优惠券更新")
    @PostMapping("/admin/marketing/coupon/info/update")
    public Result<Void> updateCoupon(
            @RequestBody @Valid UpdateCouponParam Param) {
        log.info("管理员对商家进行优惠券更新");
        couponService.updateCoupon(Param);
        return Result.success();
    }

}
